<?php
namespace app\manage\controller;

use app\manage\controller\Base;
use app\model\QueryModel;
use app\service\CateService;
use think\facade\Db;
use support\View;
use support\Request;

class QueryController extends Base
{
    public function index(Request $request)
    {

    	$key=$request->input('key','');
    	View::assign('key',$key);
		
		$query=QueryModel::where('name','like','%'.$key.'%')->order('id desc')->paginate(['list_rows'=>10,'query'=>request()->all()]);
		View::assign('query',$query);
		View::assign('count1', $query->total());
       	return view('query/index');
    }
	public function ajax(Request $request)
    {
        $data=$request->post();
		if($data['type']=='query_del'){
			$info=QueryModel::destroy($data['id']);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='query_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$info=QueryModel::saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='query_start'){
		    if(QueryModel::where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='query_stop'){
		    if(QueryModel::where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		return json(['code' => 0, 'message' => '失败']);
    }
	public function add(Request $request)
    {
        if(request()->method()=='POST'){
            $data=$request->post();
            $validate = new \app\manage\validate\QueryValidate;
            if(!$validate->check($data)){
                return view_e($validate->getError());
            }


            $file = $request->file('img');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/query/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['img']=$path;
            }
            $res=QueryModel::create($data);
            if($res){
                return view_s('添加成功',url('query/index',['st'=>1]));
            }else{
                return view_e('添加失败了');
            }
    	}
       return view('query/add');
    }
	public function edit(Request $request)
    {
        if(request()->method()=='POST'){
            $data=$request->post();
            $validate = new \app\manage\validate\QueryValidate;
            if(!$validate->check($data)){
                return view_e($validate->getError());
            }


            $file = $request->file('img');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/query/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['img']=$path;
            }
            $user = new QueryModel();
            $res=$user->update($data,['id' => $data['id']]);
            if($res){
                return view_s('修改成功',url('query/index',['st'=>1]));
            }else{
                return view_e('修改失败了');
            }
    	}

        $cid=$request->input('id',0);
        $data=Db::name('query')->where('id',$cid)->find();
        View::assign('data',$data);

        return view('query/edit');
    }
}
